package com.vecspace.dayu.code.httphandler;

import java.util.ArrayList;
import java.util.List;

import com.vecspace.jsmodel.ZTreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import com.vecspace.dayu.code.pojo.ProjectCatalog;
import com.vecspace.dayu.code.service.ProjectCatalogService;
import com.vecspace.security.model.LoginContext;
import com.vecspace.web.servlet.model.BaseController;
import com.vecspace.web.servlet.model.HController;
import com.vecspace.web.servlet.model.Param;
import com.vecspace.web.servlet.model.Path;

/** DAYU_CODE_PROJECT_CATALOG httphandler
 * 在handler中进行与页面相关的处理，比如把数据转换为HTML格式等。
 * @author wanghua
*/
@HController("dayucode_projectCatalogHandler")
public class ProjectCatalogHandler extends BaseController {

	/**根据ID查询 DAYU_CODE_PROJECT_CATALOG
	 * @return ProjectCatalog
	 * @throws Exception
	 * @author wanghua
	 */
	@Path("getProjectCatalog")
	public ProjectCatalog getProjectCatalog(@Param("id") Long id) throws Exception {
		return projectCatalogService.getProjectCatalog(id);
	}
	
//	@Path("getProjectCatalogs")
//	public List<ProjectCatalog> getProjectCatalogs() throws Exception{
//		return projectCatalogService.getProjectCatalogs();
//	}
	
	@Path("getChildProjectCatalogs")
	public List<ProjectCatalog> getChildProjectCatalogs(@Param("pId") Long pId) throws Exception{
		return projectCatalogService.getChildProjectCatalogs(pId);
	}

	@Path("getChildProjectCatalogsNode")
	public List<ZTreeNode> getChildProjectCatalogsNode(@Param("pId") Long pId) throws Exception{
		List<ZTreeNode> nodes = new ArrayList<>();
		List<ProjectCatalog> catalogs = projectCatalogService.getChildProjectCatalogs(pId);
		for(ProjectCatalog catalog : catalogs){
			ZTreeNode node = new ZTreeNode();
			node.setId("cat-"+catalog.getId());
			node.setpId("cat-" + pId);
			node.setName(catalog.getName());
			node.setIsParent(true);
			nodes.add(node);
		}
		return nodes;
	}

	/** 插入或修改 DAYU_CODE_PROJECT_CATALOG 
	 * @param item	要保存的对象
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	@Path("saveProjectCatalog")
	public ProjectCatalog saveProjectCatalog(@Param("item") ProjectCatalog item) throws Exception {
		if (item == null){
			return null;
		}
		/*** 对item对象进行校验，如必填项，格式，逻辑关系等 ***/
		String loginUserId = LoginContext.getLoginId();
		return projectCatalogService.saveViewProjectCatalog(item, loginUserId);
	}

	/** 根据ID删除 DAYU_CODE_PROJECT_CATALOG 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	@Path("deleteProjectCatalog")
	public void deleteProjectCatalog(@Param("id") Long id) throws Exception {
		projectCatalogService.deleteProjectCatalog(id);
	}

	/************* property ********************/
	@Autowired
	private ProjectCatalogService projectCatalogService;

}
